
package services.implementation;
import entity.Notes;
import java.util.List;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.mail.MessagingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Stateless
public class TimerSessionBean {

    @PersistenceContext(unitName = "snPU")
    private EntityManager em;
    String subject = "Expires Note";
    String fromAddress = "ShortNotes";
        
    @Schedule(dayOfWeek="0-6")
    public void delet5() throws MessagingException {
        SendMail mailClient = new SendMail();            
        List<Notes> list_notes = this.em.createNamedQuery("Notes.findExpiresNotes").getResultList();
        
        for (int i=0; i < list_notes.size(); i++){
            String message = "Hello " + list_notes.get(i).getIdList().getIdUser().getLogin()
                    + ", you have expires note! \n"
                    + "Text: " + list_notes.get(i).getText();
        System.out.println(message);
        System.out.println(list_notes.get(i).getIdList().getIdUser().getEmail().toString());
        
            mailClient.postMail(new String[]{
                list_notes.get(i).getIdList().getIdUser().getEmail().toString()
            }, subject, message, fromAddress);
        }
    }
    
}